$(document).ready(function(){
	$('.pf').inputmask({"mask":"0.99", "greedy": false });
	$('.multiplier').change(function(){
		if($(this).val()==0){
			$(this).val();
			$.notify('Multiplier can not be zero or blank','error');
		}
	});
	$('.numeric, .multiplier').keyup(function(){
		var _val=$(this).val();
		var _length=_val.length;
		var ln=_length-1;
		if(isNaN($(this).val())){
			$(this).val(_val.slice(0,ln));
		}
	});
	$('form').find('input, select, textarea ').each(function (){
		if($(this).hasClass('required')){
			$(this).parent().append("<span style='color:red; padding:5px'>*Required</span>");
		}
	});
	$('#refreshDueChart').click(function (){
		if(!confirm("Do you want to refresh ?")) return;
		$.ajax({
			beforeSent:$('#refreshDueChart').val("Please Wait...."),
			url:"ajax.php?action=refreshDueChart",
			success:function(data){
				location.reload();
			}
		});
	});
	
});


// Define the goTo function

(function($) {
    $.fn.goTo = function() {
        $('html, body').animate({
            scrollTop: ($(this).offset().top-100) + 'px'
        }, 'fast');
        return this; // for chaining...
    }
})(jQuery);

function highlightFieldForError(r,name) {
	
	if(r==null) {
		r = '';
	} else {
		r= r+"_";
	}

	// Make the element visible
	if(!isVisible("#"+r+name)) {
		$("#"+r+name).goTo();
	}
	
	// Focus the element
	$("#"+r+name).focus();
	
	// Highlight with colour
	$("#"+r+name).animate({backgroundColor:'#FFFF88'},0000,function(){
		$("#"+r+name).animate({backgroundColor:'white'},4000,function(){});
	});
}



//function to generate sample of profiling run format
function generateSampleDownload(v){
	var id=$('#mId').val();
	if(v==-1)
		$('#downloadLink').empty();
	else
		$('#downloadLink').html('<a href="ajax.php?action=generateSampleDownload&mId='+id+'&val='+v+'">download sample</a>');
	
}

function checkPassword(_this,password){
	if($(_this).val()!="" && $(_this).val()!=password){
		$.notify("password did not match","error");
		$(_this).val("");
		$(_this).focus();
		return false;
	}
}


//function to check unique code of Entity
var delay;
function checkUniqueCodeForEntity(_this){
	
	var code=_this.value;
	code=code.trim();
	
	if(code==""){
		$(_this).parent().find('span').html("*Required");
		$('#createEntity').attr('disabled','disabled');	
		$('#createEntity').addClass('disable');	
	} else {
		if(delay) {
				clearTimeout(delay);
			}
		delay=setTimeout(function(){
			$.ajax({
					url:"ajax.php?action=checkForUniqueCodeForEntity&code="+code,
					beforeSent:$(_this).parent().find('span').html('<img style="height:15px; width:15px" src="../../images/spinner.gif"></img> <span style="color:#00BA8B" >Checking Tag</span>'),
					success:function(data){
						res=JSON.parse(data);
						if(_this.value!=res.code) return;
						
						var goodToGo=res.result;
						
						if(goodToGo=='1'){
							$(_this).parent().find('span').html("<img style='padding:5px' height='18px' width='18px' src='../../images/tick.ico' /> </span>");
							$('#createEntity').removeAttr('disabled');
							$('#createEntity').removeClass('disable');	
						}else{
							$.notify('code should be unique','error');
							$(_this).parent().find('span').html("<img style='padding:5px' height='18px' width='18px' src='../../images/cross.ico' /> </span>");
							$('#createEntity').attr('disabled','disabled');
							$('#createEntity').addClass('disable');	
						}
					}
				});
		},500);
	}
}

//unique email id check
	$(document).ready(function(){
		var latestVal="";
		$('#adduserForm #email').keyup(function(){
			$('#adduserForm input[type=submit]').attr('disabled','disabled');
			latestVal=$(this).val();
		});
		$('#adduserForm #email').change(function(){
			_this=$(this);
			var emailId=$(this).val();
			var entityId=$("#entityId").val();
			$.ajax({
				url:"ajax.php?action=checkForUniqueEmail&emailId="+emailId+"&entityId="+entityId,
				beforeSent:$(_this).parent().find('span').html('Checking Tag'),
				success:function(res){
					var goodToGo=res.trim();
					if(goodToGo=='1'){
						$(_this).parent().find('span').html("<img style='padding:5px' height='18px' width='18px' src='../../images/tick.ico' /> </span>");
						$('#adduserForm input[type=submit]').removeAttr('disabled');
					}else{
						$.notify('This email id already exists','error');
						$(_this).parent().find('span').html("<img style='padding:5px' height='18px' width='18px' src='../../images/cross.ico' /> </span>");
						$('#adduserForm input[type=submit]').attr('disabled','disabled');
					}
				}
			});
		
		});
		
	});
	
	
function passwordResetEmail(id){
	$.ajax({
		beforeSent:$("#sendPassLink").val("sending.."),
		url:"ajax.php?action=passwordResetEmail&id="+id,
		success:function(res){
			var response=JSON.parse(res);
			$.notify(response['msg'],response['status']);
			$("#sendPassLink").val("send email");
		}
	});
}

//from reset password form of email link
function changePassword(uId){
	var p=$.trim($("#password").val());
	var cp=$.trim($("#passwordRecheck").val());
	if(p!="" && cp!="" && p==cp){
		$.ajax({
			beforeSent:$("#sendPassLink").val("sending.."),
			url:"ajax.php?action=changePassword&uId="+uId+"&psw="+p,
			complete:function(res){
				var response=JSON.parse(res);
				$("#content").html(response['msg']);
			}
		});
	
	}else{
		$.notify("Enter password and confirm password correctly", "error")
	}
}
$(document).ready(function(){
	
	$("#member_selection").change(function(){
		var _val=$.trim($(this).val());
		if(_val==""){
			$("#dueAmtBrkUpDiv").hide();
			$("#curntMaintAmt").html("");
			$("#additionalCharges").html("");
			$("#amtAlreadyPayed").html("");
			$("#previousDue").html("");
			$("#subTotal").html("");
			return;
		}
		console.log("val is "+_val);
		$.ajax({
			url:"ajax.php?action=fillAmountSplits&mem_id="+_val,
			success:function(data){
				res=JSON.parse(data);
				//$("#dueAmtBrkUpDiv").hide();
				$("#curntMaintAmt").html(res["maintenanceAmt"]);
				$("#additionalCharges").html(res["extraAmt"]);
				$("#previousDue").html(res["lastMonthDue"]);
				$("#amtAlreadyPayed").html(res["totalAmtPayed"]);
				$("#total").html(res["total_amt_payable"]);
				$("#dueAmtBrkUpDiv").show();
			}
		});
	});
});


